package com.ztgf.message.api.cms;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.message.model.bo.MessageAnnouncementBO;
import com.ztgf.message.model.bo.MessageAnnouncementSaveBO;
import com.ztgf.message.model.dto.MessageAnnouncementDTO;
import com.ztgf.message.model.service.IMessageAnnouncementService;
import com.ztgf.message.model.vo.MessageAnnouncementPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 公告消息控制器
 *
 * @author YUE
 * @Date 2020-05-28 16:20:54
 */

@RestController
@RequestMapping("/cms/messageAnnouncement")
@Slf4j
@Api(value = "MessageAnnouncementController", tags = "通知消息接口")
public class MessageAnnouncementController {

    @DubboReference
    private IMessageAnnouncementService messageAnnouncementService;

    @PostMapping("/updatePhById")
    @ApiOperation(value = "消息恢复根据Id", notes = "消息恢复根据Id")
    public Result updatePhById(Long id) {
        if (null == id)
            return new Result<>(CodeEnum.FAIL_100002);
        messageAnnouncementService.recoverPhById(id);

        return new Result<>();
    }

    @PostMapping("/deletePhById")
    @ApiOperation(value = "物理删除根据Id", notes = "物理删除根据Id")
    public Result deletePhById(Long id) {
        if (null == id)
            return new Result<>(CodeEnum.FAIL_100002);
        messageAnnouncementService.deletePhById(id);

        return new Result<>();
    }

    @ApiOperation(value = "添加通知消息", notes = "添加通知消息")
    @PostMapping(value = "/insert")
    public Result insert(MessageAnnouncementSaveBO saveBO) {
        boolean flag = messageAnnouncementService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "给所有店铺发送消息", notes = "给所有店铺发送消息")
    @PostMapping(value = "/insertAllShopsMessage")
    public Result insertAllShopsMessage(MessageAnnouncementBO bo, HttpServletRequest request) {
        //查询所有的店铺信息
        boolean b = messageAnnouncementService.insertAllShopsMessage(bo, request.getHeader("sessionId"));
        if (b) {
            return new Result();
        }
        return new Result(CodeEnum.FAIL_100007);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存通知消息", notes = "保存通知消息")
    public Result save(MessageAnnouncementSaveBO saveBO) {
        if (saveBO.getId() == null) {
            //添加
            return insert(saveBO);
        } else {
            return update(saveBO);
        }
    }

    @ApiOperation(value = "修改通知消息", notes = "修改通知消息")
    @PostMapping("/update")
    public Result update(MessageAnnouncementSaveBO saveBO) {
        MessageAnnouncementDTO dto = new MessageAnnouncementDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = messageAnnouncementService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    /**
     * 消息的Id
     *
     * @param id
     * @return
     */
    @PostMapping("/updateMessageStatus")
    @ApiOperation(value = "更新消息状态", notes = "更新消息状态")
    public Result updateMessageStatus(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean b = messageAnnouncementService.updateMessageStatusById(id);
        return new Result();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<MessageAnnouncementDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        MessageAnnouncementDTO dto = messageAnnouncementService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "系统消息列表", notes = "系统消息列表")
    @PostMapping("/queryPageList")
    public Result<Page<MessageAnnouncementDTO>> queryPageListByShopId(MessageAnnouncementBO bo) {
        Page<MessageAnnouncementDTO> page = messageAnnouncementService.getBySelectBannerInfoPage(bo);
        return new Result<>(page);
    }

    @ApiOperation(value = "发布的服务消息列表", notes = "发布的服务消息列表")
    @PostMapping("/queryPublicMessagePageList")
    public Result<?> queryPublicMessagePageList(MessageAnnouncementBO bo) {
        Page<MessageAnnouncementPageVO> page = messageAnnouncementService.queryAdminPageList(bo);
        return new Result<>(page);
//        Page<MessageAnnouncementDTO> page = messageAnnouncementService.getBySelectPublicMessageInfoPage(bo);
//        return new Result<>(page);
    }

    /**
     * 更新所有消息状态为已读
     *
     * @param messageAnnouncementBO
     * @return
     */
    @PostMapping("/updateAllMessageStatus")
    @ApiOperation(value = "更新所有消息状态", notes = "更新所有消息状态")
    public Result updateAllMessageStatus(MessageAnnouncementBO messageAnnouncementBO) {
        if (null == messageAnnouncementBO) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        if (null == messageAnnouncementBO.getShopId())
            return new Result<>(CodeEnum.FAIL_100002);
        if (null == messageAnnouncementBO.getType())
            return new Result<>(CodeEnum.FAIL_100002);
        if (null == messageAnnouncementBO.getShopType())
            return new Result<>(CodeEnum.FAIL_100002);
        messageAnnouncementService.updateAllMessageStatusByIds(messageAnnouncementBO.getShopId(), messageAnnouncementBO.getType(), messageAnnouncementBO.getShopType());
        return new Result();
    }

    /**
     * 删除已读取的消息
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteMessageByIds")
    @ApiOperation(value = "删除选中消息", notes = "删除选中消息")
    public Result deleteMessageByIds(String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        messageAnnouncementService.deleteLogicById(ids);
        return new Result();
    }

    /**
     * 删除已读取的消息
     *
     * @param messageAnnouncementBO
     * @return
     */
    @PostMapping("/deleteAllMessageStatus")
    @ApiOperation(value = "删除所有消息已读", notes = "删除所有消息已读")
    public Result deleteAllMessageStatus(MessageAnnouncementBO messageAnnouncementBO) {
        if (null == messageAnnouncementBO) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        if (null == messageAnnouncementBO.getShopId())
            return new Result<>(CodeEnum.FAIL_100002);
        if (null == messageAnnouncementBO.getType())
            return new Result<>(CodeEnum.FAIL_100002);
        if (null == messageAnnouncementBO.getShopType())
            return new Result<>(CodeEnum.FAIL_100002);
        messageAnnouncementService.deleteAllMessageStatusByIds(messageAnnouncementBO.getShopId(), messageAnnouncementBO.getType(), messageAnnouncementBO.getShopType());
        return new Result();
    }
}
